//
//  AddFoodClassVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/11/20.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "AddFoodClassVC.h"

@interface AddFoodClassVC ()
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation AddFoodClassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    
    UIButton *savebtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [savebtn setTitle:@"保存" forState:0];
    [savebtn setTitleColor:kBlackColor forState:0];
    [savebtn addTarget:self action:@selector(saveFoodClass) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:savebtn];
    
    if(_dataModel){
        _textField.text = _dataModel.category_name;
        [self setNavTitle:@"编辑菜品分类" titleColor:[UIColor blackColor] font:font17];
    }else{
       [self setNavTitle:@"新增菜品分类" titleColor:[UIColor blackColor] font:font17];
    }
    
    __weak typeof (self)weakself = self;
    [self.backBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        if(_dataModel && ![weakself.dataModel.category_name isEqualToString:weakself.textField.text]){
            [weakself backAction];
        }else{
           [weakself.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (void)backAction{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否保存修改的信息" preferredStyle:UIAlertControllerStyleAlert];
    __weak typeof (self)weakSelf = self;
    [alertController addAction:[UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf saveFoodClass];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)saveFoodClass{
    if(_textField.text.length<1){
        [AlertUtils showToastWithMessage:@"商品分类不能为空" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
    NSDictionary *dic = @{};
    if(_dataModel){
        dic = @{@"category_id":_dataModel.categoryID,@"category_name":_textField.text};
    }else{
       dic = @{@"category_name":_textField.text};
    }
    
    __weak typeof (self)weakSelf = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/GoodsCate/categorySave" isactive:NO parameter:dic successBlock:^(PPHttpRequest *request, NSArray* responseObj) {
        
        [AlertUtils showToastWithMessage:@"保存成功" inView:nil hideAfterDelay:2.0 beIndeter:NO];
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        
    }];

}

@end
